package com.springcloud.apigateway.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;


@Slf4j
@Component
public class ThrowExceptionFilter extends ZuulFilter {

    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() {
        log.info("this is a pre filter, it will throw a RuntimeException");
        RequestContext currentContext = RequestContext.getCurrentContext();
//        try {
            doSomeThing();
//        } catch (Exception e) {
//            currentContext.set("error.status_code", HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
//            currentContext.set("error.exception", e);
//            e.printStackTrace();
//        }
        return null;
    }

    private void doSomeThing() {
        throw new RuntimeException("exist some errors");
    }
}
